﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Delay;
using System.ComponentModel;

namespace PhoneInfo.Page
{
    public partial class PK : PhoneApplicationPage
    {
        private ProgressIndicator _progress = new ProgressIndicator() { IsVisible = false, IsIndeterminate = true };
        private phonePkView pkView = null;
        public PK()
        {
            InitializeComponent();
            this.SetValue(SystemTray.OpacityProperty, 0.00001);
            Loaded += new RoutedEventHandler(this.PK_Loaded);
        }
        private void PK_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.ProgressIndicator = this._progress;
            this._progress.IsVisible = true;
            this.pkView = new phonePkView();
            this.pkView.PropertyChanged += new PropertyChangedEventHandler(this.shrewd_PropertyChanged);
            this.pkView.ImageVis = false;
            this.pkView.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(this.pkView.GetPhoneInfoUrl()));
            this.lst.DataContext = this.pkView;
            if (App.appSitting.Theme == 1)
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBar"];
            else
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBar1"];
        }
        private void ListBox_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null) return;
            ListBox lb = sender as ListBox;
            if (lb == null) return;
            phonePkItem pkItem = lb.SelectedItem as phonePkItem;
            if (pkItem == null) return;
            string objective = "/Page/PhonePk.xaml";
            objective += "?pid1=" + pkItem.Ponhe1_pid;
            objective += "&name1=" + pkItem.Ponhe1_ppname + "\r\n" + pkItem.Ponhe1_phonename;
            objective += "&phonename1=" + pkItem.Ponhe1_phonename;
            objective += "&ppname1=" + pkItem.Ponhe1_ppname;
            objective += "&picurl1=" + pkItem.Ponhe1_picurl;
            objective += "&jia1=" + pkItem.Ponhe1_price;
            objective += "&pid2=" + pkItem.Ponhe2_pid;
            objective += "&ppid1=" + pkItem.Ponhe1_ppid;
            objective += "&ppid2=" + pkItem.Ponhe2_ppid;
            objective += "&picurl2=" + pkItem.Ponhe2_picurl;
            objective += "&name2=" + pkItem.Ponhe2_ppname + " " + pkItem.Ponhe2_phonename;
            objective += "&jia2=" + pkItem.Ponhe2_price;
            objective += "&phonename2=" + pkItem.Ponhe2_phonename;
            objective += "&ppname2=" + pkItem.Ponhe2_ppname;
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }
        private void shrewd_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null) return;
            phonePkView pk = sender as phonePkView;
            if ((pk != null) && (e.PropertyName == "ImageVis"))
            {
                if (pk.ImageVis)
                    this._progress.IsVisible = false;
                if (!pk.ImageVis)
                    this._progress.IsVisible = true;
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Page/PhonePk.xaml?optional=1", UriKind.Relative));
        }
    }
}